import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle


class GridMap:
    def __init__(self, length, width):
        self.length = length
        self.width = width
        self.MapList = []
        self.setInitMap()
        # self.setObstacles()

    def setInitMap(self):

        for i in range(self.width):
            for j in range(self.length):
                self.MapList.append([i, j, 1])

    def setObstacles(self):
        obstacle = []
        obstacle_dict = {
            'ob1': [[0, 0], [0, 8]],
            'ob2': [[1, 0], [9, 0]],
            'ob3': [[9, 1], [9, 8]],
            'ob4': [[0, 9], [9, 9]]
        }
        obstacles_list = [obstacle_dict[key] for key in obstacle_dict]
        for i in range(len(obstacles_list)):
            length = obstacles_list[i][1][0] - obstacles_list[i][0][0] + 1
            width = obstacles_list[i][1][1] - obstacles_list[i][0][1] + 1
            for j in range(length):
                for k in range(width):
                    obstacle.append([obstacles_list[i][0][0] + j, obstacles_list[i][0][1] + k])

        for i in range(len(self.MapList)):
            if [self.MapList[i][0], self.MapList[i][1]] in obstacle:
                self.MapList[i][2] = -1


if __name__ == '__main__':
    map1 = GridMap(10, 10)
    print(map1.MapList)
    fig, ax = plt.subplots(figsize=(5, 5))
    ax.set_xlim([0, map1.length])
    ax.set_ylim([0, map1.width])
    for i in range(len(map1.MapList)):

        if map1.MapList[i][2] == -1:
            rec = Rectangle((map1.MapList[i][0], map1.MapList[i][1]), width=1, height=1, color='gray')
            ax.add_patch(rec)
        else:
            rec = Rectangle((map1.MapList[i][0], map1.MapList[i][1]), width=1, height=1, edgecolor='gray',
                            facecolor='w')
            ax.add_patch(rec)

    plt.axis("equal")
    plt.axis('off')
    plt.tight_layout()
    plt.pause(0.01)
    print("绘图结束")
    plt.show()
